package com.twq;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author twq
 * @date 2021/8/26 17:54
 */
//代码自动生成器
public class kuangCode {
    public static void main(String[] args) {
        //需要构建一个代码自动生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("twq");
        gc.setOpen(false);
        //是否覆盖
        gc.setFileOverride(false);
        //去service的前缀的I前缀
        gc.setServiceName("%sService");
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);//配置swagger

        mpg.setGlobalConfig(gc);

        //2，设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=true&useUnicode=true");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        //3，包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("blog");
        pc.setParent("com.twq");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //4，策略配置
        StrategyConfig sc = new StrategyConfig();
        //设置要映射的表名
        sc.setInclude("user");

        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        //sc.setSuperEntityClass("");
        //自动Lombok
        sc.setEntityLombokModel(true);
        //sc.setRestControllerStyle(true);

        //逻辑删除字段
        sc.setLogicDeleteFieldName("deleted");

        //自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> listFill = new ArrayList<>();
        listFill.add(createTime);
        listFill.add(updateTime);
        sc.setTableFillList(listFill);

        //乐观锁
        sc.setVersionFieldName("version");
        //restfull驼峰命名
        sc.setRestControllerStyle(true);
        //多字段转下划线   localhost:8080/hello_id_2
        sc.setControllerMappingHyphenStyle(true);

        mpg.setStrategy(sc);

       //执行
        mpg.execute();
    }
}
